Restore the CoreFoundation Swift overlay on non-Darwin - #5519
Conversation
compnerd
left a comment
There was a problem hiding this comment.
Why is the test limited to Linux? Can you split up Hashable on _CFObject? That allows you to more easily see the implementation of the requirements. Also, make Equatable explicit on the protocol as an explicit extension with the operator implementation.
|
Thanks for the review. @compnerd
The test prepends the freshly built Foundation library directory to I limited the runnable test to Linux because that is the environment in which I could validate the dynamic-library setup. I do not currently have a local Windows environment to verify the corresponding test behavior. If you have a convenient Windows setup, I would very much welcome a commit on this PR that adapts the runtime setup as needed and removes the Linux-only restriction for Windows.
Do you mean something like this? // MARK: - _CFObject
public protocol _CFObject: AnyObject, Equatable, Hashable {}
// MARK: - _CFObject + Equatable
extension _CFObject {
public static func == (lhs: Self, rhs: Self) -> Bool {
CFEqual(lhs, rhs)
}
}
// MARK: - _CFObject + Hashable
extension _CFObject {
public var hashValue: Int {
Int(bitPattern: CFHash(self))
}
public func hash(into hasher: inout Hasher) {
hasher.combine(hashValue)
}
} |
|
Looks like the new test case I added here will not run by the CI since it is swiftpm based test. I'll create a swiftlang/swift multi-repo PR to update test entry and coordinate it. |
My environment is currently setup for COM work, so it is difficult to switch.
You can also move the conformance here:
Likewise. |
No. Protocol is not allowed to do so IIRC. @compnerd And I test it: protocol A {}
extension A: Hashable { // ❌ Extension of protocol 'A' cannot have an inheritance clause
...
}
Got it. I'll try it without platform limitation first. If I can solve the CI issue that's fine. If windows platform is bumping some strange issues, I'll decide what to do then. |
Wait a minute - why is this a goal? |
|
We specifically do not want people to use the C library on non-Darwin platforms. The Swift library is the API. The C library is an implementation detail (and one we are working towards removing by reimplementing its functionality in Swift). |
I completely agree with the long-term direction. My point is simply that, today, the C library is still available on Linux and Android, and many existing packages depend on it. We should certainly deprecate it gradually and encourage migration. But as long as it's still part of the shipped implementation, fixing inconsistencies seems worthwhile. It improves compatibility for existing code without encouraging new dependencies on the C API. @parkera |
|
@Kyle-Ye another option: follow Windows and later platforms and drop CF accessibility. The C interfaces are an implementation detail that are not meant to be available for users of Foundation. |
Restore the CoreFoundation Swift overlay on non-Darwin platforms so imported
Core Foundation reference types regain their
EquatableandHashableconformances.
Motivation:
On Darwin, imported Core Foundation reference types conform to the
compiler-known
CoreFoundation._CFObjectprotocol. That protocol inheritsHashableand supplies equality and hashing withCFEqualandCFHash.The non-Darwin Core Foundation module is currently Clang-only and does not
provide
_CFObject. Although the Clang importer still synthesizes theconformance for recognized CF reference types, it has no protocol to attach,
leaving types such as
CFDictionary,CFArray,CFSet, andCFStringwithout
EquatableorHashableconformance.Fixes #5518.
Modifications:
CoreFoundationSwift overlay that restores_CFObjectand its
CFEqual/CFHashdefault witnesses.named
CoreFoundation, then link its implementation intoFoundation.CoreFoundation.swiftmoduleand.swiftdoc, includingthe static toolchain layout and required autolink behavior.
which cannot mix the existing C sources and the new Swift source in one
target.
Foundation reference types, plus runtime equality and set deduplication for
CFDictionary.Result:
Clients importing
CoreFoundationon non-Darwin platforms can use recognizedCore Foundation reference types as
EquatableandHashable, matching theDarwin overlay behavior. The fix applies to the CF reference type family rather
than adding a
CFDictionary-specific retroactive conformance inFoundation.Testing:
client importing only
CoreFoundation.libFoundation.sobuild and passedCoreFoundation.Hashablewith CTest.CFDictionary,CFMutableDictionary,CFArray,CFMutableArray,CFSet,CFMutableSet,CFString,CFMutableString,CFData, andCFMutableDatasatisfyHashable.autolinks both
Foundationand the separateCoreFoundationarchive.swift package dump-packageandgit diff --check.